package com.easyexcell.easyexcellexample.utils.esayexcell.handler;

import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 设置单元格是否启用
 *
 * @User c.c
 * @Since 2023/9/1
 * @Time 17:13
 * @Description
 */

@Slf4j
public class UnLockSheetWriteHandler extends LongestMatchColumnWidthStyleStrategy {
  @Override
  public int order() {
    return 1000001;
  }

  @Override
  public void afterCellDispose(CellWriteHandlerContext context) {
    //哪些单元格放开锁
    Cell cell = context.getCell();
    Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
    CellStyle cellStyle = workbook.createCellStyle();
    if (cell.getColumnIndex() == 7 || cell.getColumnIndex() == 8 || cell.getColumnIndex() == 12) {
      log.info("第" + cell.getRowIndex() + "行,第" + cell.getColumnIndex() + "解锁");
      cellStyle.setLocked(false);
      context.getCell().setCellStyle(cellStyle);
    }
    //设置未上锁的格式
    if (cellStyle.getLocked()) {
      Font font = cell.getSheet().getWorkbook().createFont();
      font.setColor(IndexedColors.GREY_40_PERCENT.index);
      cellStyle.setFont(font);
      context.getCell().setCellStyle(cellStyle);
    }
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    context.getCell().setCellStyle(cellStyle);
  }
}
